use std::mem::MaybeUninit;
use std::{mem, ptr};

#[derive(Debug)]
struct Member {
    msg: String,
}

impl Member {
    pub fn new(msg: &str) -> Self {
        Self {
            msg: msg.to_owned(),
        }
    }
}

impl Drop for Member {
    fn drop(&mut self) {
        println!("Member dropped now -> {:?}", self)
    }
}

#[test]
fn test_maybe_uninit() {
    //初始化一个空结构的Member
    let mut mm = MaybeUninit::<Member>::uninit();
    //想改结构写入Member，但是不会触发drop，所有权沿用入参所有权
    let x = mm.write(Member::new("shit"));
    unsafe {
        //此时read会导致拷贝，出现多所有权，会有Double Drop
        let mem_read = mm.assume_init_read();
        //read和write一定要一起出现，别面Double Drop
        //warn 这里的write不是直接拷贝，而是通过MaybeUninit赋值，此时会导致原变量没有释放（内部维护的是ManuallyDrop）
        mm.write(Member::new("you"));
        //此时将真正的所有权转移给member
        let member = mm.assume_init();
    }
}
